package PersonalHomepage;

import Change.ChangeInfo;
import Change.ChangePassword;
import EncapsulatingObjects.User;
import LoginSystem.LoginPage;
import Manage.ViewAllTrip;
import ShoppingCart.Shop;
import TextDocument.UserAgreement;
import Util.UiUtil;
import Util.UserDatabaseUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UserHomepage {
    static JFrame jf;
    JMenuBar jb;
    JMenu home;
    JMenu mUser;
    JMenu shopCar;
    JMenu help;

    JMenuItem myHome;
    JMenuItem cInfo;
    JMenuItem cPass;
    JMenuItem mlogout;

    JLabel photo;//头像

    JLabel account;//账号

    JLabel name;//姓名
    JLabel name1;//姓名

    JLabel phone;//电话号码
    JLabel phone1;//电话号码

    JLabel gender;//性别
    JLabel gender1;//性别

    JLabel birthday;//生日
    JLabel birthday1;//生日

    JLabel signature;//签名
    JTextArea signature1;//签名

    JPanel info;//左侧用户信息容器
    JPanel operation;//右侧操作容器

    JButton shoppingCart;//购物车
    JButton viewOrder;//查看所有订单，已支付和未支付的
    JButton viewAllTrip;//旅行足迹
    JButton changeInfo;//更改信息
    JButton changePass;//更改密码
    JButton logOut;//退出登录

    UserDatabaseUtil du = new UserDatabaseUtil();
    User user = new User();
    UiUtil ui = new UiUtil();

    public UserHomepage() {
        init();
        ui.setFrameCenter(jf);
        ui.setFrameImage(jf);
    }

    private void initInfo() {
        name1 = new JLabel(user.getName(), JLabel.CENTER);
        name1.setOpaque(true);
        name1.setBackground(Color.WHITE);
        phone1 = new JLabel(user.getPhone(), JLabel.CENTER);
        phone1.setOpaque(true);
        phone1.setBackground(Color.WHITE);
        gender1 = new JLabel(user.getGender(), JLabel.CENTER);
        gender1.setOpaque(true);
        gender1.setBackground(Color.WHITE);
        birthday1 = new JLabel(String.valueOf(user.getBirthday()), JLabel.CENTER);
        birthday1.setOpaque(true);
        birthday1.setBackground(Color.WHITE);

        if (user.getSignature() == null)
            signature1 = new JTextArea("write your personal signature");
        else
            signature1 = new JTextArea(user.getSignature());
        signature1.setEditable(false);
        signature1.setLineWrap(true);
        signature1.setWrapStyleWord(true);
        signature1.setOpaque(true);
        signature1.setBackground(Color.WHITE);
    }

    private void init() {
        jf = new JFrame("USER HOME PAGE");
        jf.setLayout(null);
        jf.setResizable(false);

        home = new JMenu("View All Trip");
        home.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new Shop();
            }
        });
        myHome = new JMenuItem("My Homepage");
        cInfo = new JMenuItem("Change Information");
        cPass = new JMenuItem("Change Password");
        mlogout = new JMenuItem("Log Out");
        jb = new JMenuBar();
        jb.setBackground(new Color(232, 232, 232));

        mUser = new JMenu("User");
        shopCar = new JMenu("Shopping Cart");
        shopCar.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                goShopCar();
            }
        });
        help = new JMenu("Help Me");
        help.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                UserAgreement.isHelp = true;
                new UserAgreement();
            }
        });

        cInfo = new JMenuItem("Change Information");
        cInfo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                goChangeInfo();
            }
        });
        cPass = new JMenuItem("Change Password");
        cPass.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                goChangePass();
            }
        });
        mlogout = new JMenuItem("Log out");
        mlogout.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                goLogout();
            }
        });

        info = new JPanel();
        info.setVisible(true);
        info.setBackground(new Color(238, 229, 222));
        operation = new JPanel();
        operation.setVisible(true);
        operation.setBackground(Color.white);

        info.setLayout(null);
        operation.setLayout(null);

        photo = new JLabel();
        UiUtil.setPhoto1(photo);

        name = new JLabel("NAME", JLabel.CENTER);
        phone = new JLabel("PHONE NUMBER", JLabel.CENTER);
        gender = new JLabel("GENDER", JLabel.CENTER);
        birthday = new JLabel("BIRTHDAY", JLabel.CENTER);
        signature = new JLabel("PERSONAL SIGNATURE", JLabel.CENTER);

        user = du.initUser(user);

        account = new JLabel(user.getAccount(), JLabel.CENTER);
        account.setOpaque(true);
        account.setBackground(Color.WHITE);

        initInfo();

        shoppingCart = new JButton("My Shopping Cart");
        shoppingCart.setOpaque(true);
        shoppingCart.setBackground(new Color(79, 125, 184));
        shoppingCart.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                goShopCar();
            }
        });
        viewOrder = new JButton("View paid orders");
        viewOrder.setOpaque(true);
        viewOrder.setBackground(new Color(79, 125, 184));
        viewOrder.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                ViewAllTrip.buttonName = "Refresh";
                new ViewAllTrip();
            }
        });

        viewAllTrip = new JButton("View all trips");
        viewAllTrip.setOpaque(true);
        viewAllTrip.setBackground(new Color(79, 125, 184));
        viewAllTrip.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new Shop();
            }
        });
        changeInfo = new JButton("Change information");
        changeInfo.setOpaque(true);
        changeInfo.setBackground(new Color(79, 125, 184));
        changeInfo.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                goChangeInfo();
            }
        });
        changePass = new JButton("change password");
        changePass.setOpaque(true);
        changePass.setBackground(new Color(79, 125, 184));
        changePass.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                goChangePass();
            }
        });

        logOut = new JButton("Log out");
        logOut.setOpaque(true);
        logOut.setBackground(new Color(245, 132, 132));

        jf.setSize(800, 628);
        jb.setBounds(0, 0, 800, 35);
        info.setBounds(1, 30, 300, 570);
        operation.setBounds(301, 30, 500, 570);

        photo.setBounds(100, 10, 100, 120);
        photo.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        account.setBounds(50, 135, 200, 20);

        name.setBounds(50, 170, 200, 20);
        name1.setBounds(50, 195, 200, 20);
        phone.setBounds(50, 220, 200, 20);
        phone1.setBounds(50, 245, 200, 20);
        gender.setBounds(50, 270, 200, 20);
        gender1.setBounds(50, 295, 200, 20);
        birthday.setBounds(50, 320, 200, 20);
        birthday1.setBounds(50, 345, 200, 20);
        signature.setBounds(50, 370, 200, 20);
        signature1.setBounds(50, 395, 200, 100);

        shoppingCart.setBounds(50, 50, 400, 55);
        shoppingCart.setFont(new Font("Times New Roman", Font.BOLD, 30));
        viewOrder.setBounds(50, 130, 400, 55);
        viewOrder.setFont(new Font("Times New Roman", Font.BOLD, 30));
        viewAllTrip.setBounds(50, 210, 400, 55);
        viewAllTrip.setFont(new Font("Times New Roman", Font.BOLD, 30));
        changeInfo.setBounds(50, 290, 400, 55);
        changeInfo.setFont(new Font("Times New Roman", Font.BOLD, 30));
        changePass.setBounds(50, 370, 400, 55);
        changePass.setFont(new Font("Times New Roman", Font.BOLD, 30));

        logOut.setBounds(142, 460, 200, 55);
        logOut.setFont(new Font("Times New Roman", Font.BOLD, 20));
        logOut.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                goLogout();
            }
        });

        jb.add(home);
        jb.add(mUser);
        jb.add(shopCar);
        jb.add(help);

        mUser.add(myHome);
        mUser.add(cInfo);
        mUser.add(cPass);
        mUser.add(mlogout);

        info.add(photo);
        info.add(account);
        info.add(name);
        info.add(name1);
        info.add(phone);
        info.add(phone1);
        info.add(gender);
        info.add(gender1);
        info.add(birthday);
        info.add(birthday1);
        info.add(signature);
        info.add(signature1);
        operation.add(shoppingCart);
        operation.add(viewOrder);
        operation.add(viewAllTrip);
        operation.add(changeInfo);
        operation.add(changePass);
        operation.add(logOut);

        jf.add(jb);
        jf.add(info);
        jf.add(operation);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        jf.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if(JOptionPane.showConfirmDialog(null,"Are you sure you want to exit？") == JOptionPane.YES_OPTION) {
                    du.updateLoginStatue(user, false);
                    jf.dispose();
                }
            }
        });
    }

    private void goChangePass() {
        new ChangePassword();
    }

    private void goChangeInfo() {
        jf.setBounds(jf.getX()-300,jf.getY(),800,630);
        new ChangeInfo();
    }

    private void goLogout() {
        if (JOptionPane.showConfirmDialog(jf, "Are you sure you want to log out") == JOptionPane.YES_OPTION) {
            du.updateLoginStatue(user, false);
            jf.dispose();
            new LoginPage();
        }
    }

    private void goShopCar() {
        ViewAllTrip.buttonName = "Submit";
        new ViewAllTrip();
    }

    public static void center(){
        UiUtil.setFrameCenter(jf);
    }
    public static void closePage(){
        jf.dispose();
    }
}
